package org.example.Thread;

import java.lang.Thread;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:Lenovo
 * Date:2022-03-22
 * Time:13:56
 */
public class Demo10 {
    public static int count = 0;
    public static void main(String[] args) {
        Thread t = new Thread(() ->{
            while (!Thread.currentThread().isInterrupted()){
                System.out.println("hello thread - > " + count);
                count++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
/*                    //当触发异常的时候，立即就退出循环
                    break;*/
                }
            }
        });
        t.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //在主线程中，调用interrupted方法，来中断这个线程
        //t.interrupt的意思就是t线程被中断！！！
        t.interrupt();
    }
}
